//
//  YBBlogController.m
//  Cocoachina
//
//  Created by 尚往文化 on 17/9/13.
//  Copyright © 2017年 YBing. All rights reserved.
//

#import "YBBlogController.h"
#import <MJRefresh.h>
#import <TFHpple.h>
#import "YBBlog.h"
#import "YBBlogCell.h"
#import "YBBlogDetailController.h"

@interface YBBlogController ()

@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) NSMutableArray<YBBlog *> *datas;

@end

@implementation YBBlogController


- (NSMutableArray<YBBlog *> *)datas
{
    if (_datas==nil) {
        _datas = [NSMutableArray array];
    }
    return _datas;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupTableView];
    
}

- (void)setupTableView
{
    [self.tableView registerNib:[UINib nibWithNibName:@"YBBlogCell" bundle:nil] forCellReuseIdentifier:@"YBBlogCell"];
    self.tableView.estimatedRowHeight = UITableViewAutomaticDimension;
    self.tableView.rowHeight = 159;
    __weak typeof(self) weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf loadData];
    }];
    
}

- (void)loadData
{
    [super loadData];
    NSString *url = [NSString stringWithFormat:@"%@1", self.url];
    [YBHttpTool get:url success:^(TFHpple *doc) {
        [self.tableView.mj_header endRefreshing];
        NSMutableArray *datas = [self parseWithHpple:doc];
        self.datas = datas;
        YBWeakSelf
        if (self.tableView.mj_footer==nil) {
            self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
                [weakSelf loadMoreData];
            }];
        }
        if (datas.count==0) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        } else {
            [self.tableView.mj_footer resetNoMoreData];
        }
        self.page = 2;
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
    }];
    
}

- (void)loadMoreData
{
    NSString *url = [NSString stringWithFormat:@"%@%zi", self.url, self.page];
    [YBHttpTool get:url success:^(TFHpple *doc) {
        [self.tableView.mj_footer endRefreshing];
        NSArray *datas = [self parseWithHpple:doc];
        [self.datas addObjectsFromArray:datas];
        if (datas.count==0) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        } else {
            self.page++;
            [self.tableView.mj_footer resetNoMoreData];
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [self.tableView.mj_footer endRefreshing];
    }];
}

- (NSMutableArray<YBBlog *> *)parseWithHpple:(TFHpple *)doc
{
    NSArray<TFHppleElement *> *elements  = [doc searchWithXPathQuery:@"//div[@class='bloglist']"];
    TFHppleElement *listElement = [elements firstObject];
    NSArray<TFHppleElement *> *list = [listElement childrenWithTagName:@"div"];
    NSMutableArray *datas = [NSMutableArray array];
    [list enumerateObjectsUsingBlock:^(TFHppleElement * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        YBBlog *blog = [YBBlog new];
        TFHppleElement *titleElement = [obj firstChildWithTagName:@"a"];
        blog.title = titleElement.text;
        blog.url = [titleElement objectForKey:@"href"];
        
        TFHppleElement *descElement = [obj firstChildWithClassName:@"summ"];
        blog.desc = descElement.text;
        
        TFHppleElement *tagsElement = [obj firstChildWithClassName:@"faces"];
        NSArray<TFHppleElement *> *tags = [tagsElement childrenWithTagName:@"span"];
        NSMutableArray<NSString *> *arr = [NSMutableArray array];
        [tags enumerateObjectsUsingBlock:^(TFHppleElement * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [arr addObject:obj.text];
        }];
        blog.tag = arr;
        
        TFHppleElement *labelsElement = [obj firstChildWithClassName:@"labels"];
        blog.author = [labelsElement firstChildWithTagName:@"a"].text;
        
        NSArray<TFHppleElement *> *lbs = [labelsElement childrenWithTagName:@"span"];
        blog.time = lbs[0].text;
        blog.likenum = lbs[1].text;
        blog.commentnum = lbs[2].text;
        blog.readnum = lbs[3].text;
        [datas addObject:blog];

    }];
    return datas;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YBBlogCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YBBlogCell" forIndexPath:indexPath];
    cell.blog = self.datas[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    YBBlogDetailController *vc = [YBBlogDetailController new];
    vc.url = self.datas[indexPath.row].url;
    vc.title = self.datas[indexPath.row].title;
    [self.navigationController pushViewController:vc animated:YES];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 9;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 9;
}

@end
